package com.CST.controller.ys;

import com.CST.common.api.ResultObject;
import com.CST.dto.ys.DataDTO;
import com.CST.service.ys.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@RestController()
@RequestMapping("/file")
@Api(tags = "文件上传")
public class FileController {

    private final FileService fileService;

    @Value("${uploadAddr}")
    private String path; // 静态资源配置url
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }
    @ApiOperation("下载申诉条目的所有文件")
    @PostMapping("/download")
    public void fileDownLoad(HttpServletResponse response, @RequestBody DataDTO dto) throws IOException {
        fileService.download(dto.getId(), response);
    }

    @ApiOperation("添加申诉文件")
    @PostMapping("/addFile")
    public ResultObject<Long> addAppeal(@RequestParam("file") MultipartFile files) throws IOException {
        return ResultObject.success(fileService.upload(files).getId());
    }

    @ApiOperation("添加申诉文件返回文件地址")
    @PostMapping("/updateFile")
    public ResultObject<String> insertFileReturnUrl(@RequestParam("file") MultipartFile files) throws IOException {
        return ResultObject.success(path+fileService.upload(files).getUuidName());
    }



}
